package cn.iocoder.yudao.module.pay.bill.controller.app;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.convert.BigDecimalConvert;
import cn.iocoder.yudao.framework.business.basic.enums.EnumCodable;
import cn.iocoder.yudao.framework.business.basic.pojo.bo.ExtraPageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
import cn.iocoder.yudao.module.api.pay.bill.enums.BillCategoryEnum;
import cn.iocoder.yudao.module.api.pay.bill.enums.BillTypeEnum;
import cn.iocoder.yudao.module.pay.bill.service.PayBillService;
import cn.iocoder.yudao.module.pay.bill.vo.AppPayBillPageReqVO;
import cn.iocoder.yudao.module.pay.bill.vo.AppPayBillRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 账单")
@RestController
@RequestMapping("/pay/bill")
@Validated
@Slf4j
public class AppPayBillController {

    @Resource
    private PayBillService payBillService;

    @GetMapping("/get-biz-types")
    @Operation(summary = "获得账单业务类型")
    public CommonResult<List<EnumCodable.ViewObject>> getBillBizTypes(@RequestParam(value = "category", required = false) String category) {
        if (StrUtil.isBlank(category)) {
            return success(EnumCodable.toVos(BillTypeEnum.values()));
        }
        if (BillCategoryEnum.CRYPTOCURRENCY_ORIGIN.getCode().equals(category)) {
            return success(EnumCodable.toVos(new BillTypeEnum[]{
                    BillTypeEnum.SYSTEM_ADJUST,
                    BillTypeEnum.CRYPTOCURRENCY_DEDUCTION,
                    BillTypeEnum.TRANSFER_INPUT,
                    BillTypeEnum.TRANSFER_OUTPUT,
                    BillTypeEnum.EXCHANGE_MONEY,
                    BillTypeEnum.CRYPTOCURRENCY_INCREMENT
            }));
        }
        if (BillCategoryEnum.COMMISSION.getCode().equals(category)) {
            return success(EnumCodable.toVos(new BillTypeEnum[]{
                    BillTypeEnum.COMMISSION_UNFREEZE,
                    BillTypeEnum.COMMISSION_CANCEL,
                    BillTypeEnum.COMMISSION_CASH_OUT,
            }));
        }
        if (BillCategoryEnum.CRYPTOCURRENCY_INCREMENT.getCode().equals(category)) {
            return success(EnumCodable.toVos(new BillTypeEnum[]{
                    BillTypeEnum.SYSTEM_ADJUST,
                    BillTypeEnum.CRYPTOCURRENCY_DEDUCTION,
                    BillTypeEnum.TRANSFER_INPUT,
                    BillTypeEnum.TRANSFER_OUTPUT,
                    BillTypeEnum.EXCHANGE_MONEY
            }));
        }
        if (BillCategoryEnum.BONUS.getCode().equals(category)) {
            return success(EnumCodable.toVos(new BillTypeEnum[]{
                    BillTypeEnum.EXCHANGE_MONEY,
                    BillTypeEnum.EXCHANGE_PRODUCT,
            }));
        }
        if (BillCategoryEnum.INTEREST.getCode().equals(category)) {
            return success(EnumCodable.toVos(new BillTypeEnum[]{
                    BillTypeEnum.EXCHANGE_MONEY,
            }));
        }
        return success(EnumCodable.toVos(BillTypeEnum.class,
                BillTypeEnum.UNKNOWN.getCode(),
                BillTypeEnum.SYSTEM_ADJUST.getCode(),
                BillTypeEnum.PAY_VIP.getCode(),
                BillTypeEnum.PAY_AGENCY.getCode(),
                BillTypeEnum.PAY_ALLIANCE.getCode(),
                BillTypeEnum.PAY_BONUS_BENEFIT.getCode(),
                BillTypeEnum.PAY_PRODUCT.getCode(),
                BillTypeEnum.PAY_PRODUCT_REFUND.getCode(),
                BillTypeEnum.PAY_AD_PLAN.getCode(),
                BillTypeEnum.PAY_OFFLINE_SCAN.getCode(),
                BillTypeEnum.CASH_OUT_SUCCESS.getCode(),
                BillTypeEnum.CASH_OUT_FAILURE.getCode()));
    }

    @GetMapping("/page")
    @Operation(summary = "获取账单", description = "收支明细")
    public CommonResult<ExtraPageResult<AppPayBillRespVO>> getBillPage(@Valid AppPayBillPageReqVO reqVO) {
        Long userId = getLoginUserId();
        PageResult<AppPayBillRespVO> page = payBillService.getBillPage(userId, reqVO);
        ExtraPageResult<AppPayBillRespVO> pageX = new ExtraPageResult<>(page.getList(), page.getTotal());
        if (List.of(BillCategoryEnum.MONEY.getCode(),
                        BillCategoryEnum.RED_PACKET.getCode(),
                        BillCategoryEnum.COMMISSION.getCode(),
                        BillCategoryEnum.BONUS.getCode(),
                        BillCategoryEnum.INTEREST.getCode(),
                        BillCategoryEnum.FEE.getCode())
                .contains(reqVO.getCategory())) {
            pageX.put("numberSum", MoneyUtils.fenToYuanStr(payBillService.getNumberSum(userId, reqVO).intValue()));
            pageX.put("inNumberSum", MoneyUtils.fenToYuanStr(payBillService.getInNumberSum(userId, reqVO).intValue()));
            pageX.put("outNumberSum", MoneyUtils.fenToYuanStr(payBillService.getOutNumberSum(userId, reqVO).intValue()));
        } else {
            pageX.put("numberSum", BigDecimalConvert.INSTANCE.convert(payBillService.getNumberSum(userId, reqVO)));
            pageX.put("inNumberSum", BigDecimalConvert.INSTANCE.convert(payBillService.getInNumberSum(userId, reqVO)));
            pageX.put("outNumberSum", BigDecimalConvert.INSTANCE.convert(payBillService.getOutNumberSum(userId, reqVO)));
        }
        return success(pageX);
    }

}

